package com.csxy.six.controller;

import com.csxy.six.dto.FoundItemDTO;
import com.csxy.six.entity.FoundItem;
import com.csxy.six.service.FoundItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/found-items")
public class FoundItemController {

    private final FoundItemService foundItemService;

    @Autowired
    public FoundItemController(FoundItemService foundItemService) {
        this.foundItemService = foundItemService;
    }

    @PostMapping
    public ResponseEntity<FoundItem> addFoundItem(@RequestBody FoundItemDTO foundItemDTO) {
        FoundItem addedItem = foundItemService.addFoundItem(foundItemDTO);
        return ResponseEntity.ok(addedItem);
    }

    @GetMapping
    public ResponseEntity<List<FoundItem>> getAllFoundItems() {
        List<FoundItem> items = foundItemService.getAllFoundItems();
        return ResponseEntity.ok(items);
    }

    @GetMapping("/{foundId}")
    public ResponseEntity<FoundItem> getFoundItemById(@PathVariable Integer foundId) {
        FoundItem item = foundItemService.getFoundItemById(foundId);
        return ResponseEntity.ok(item);
    }

    @PutMapping("/{foundId}")
    public ResponseEntity<FoundItem> updateFoundItem(
            @PathVariable Integer foundId,
            @RequestBody FoundItemDTO foundItemDTO) {
        FoundItem updatedItem = foundItemService.updateFoundItem(foundId, foundItemDTO);
        return ResponseEntity.ok(updatedItem);
    }

    @DeleteMapping("/{foundId}")
    public ResponseEntity<Void> deleteFoundItem(@PathVariable Integer foundId) {
        foundItemService.deleteFoundItem(foundId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/publisher/{publisherId}")
    public ResponseEntity<List<FoundItem>> getFoundItemsByPublisher(@PathVariable Long publisherId) {
        List<FoundItem> items = foundItemService.getFoundItemsByPublisher(publisherId);
        return ResponseEntity.ok(items);
    }

    @PatchMapping("/{foundId}/claimed")
    public ResponseEntity<FoundItem> markAsClaimed(@PathVariable Integer foundId) {
        FoundItem item = foundItemService.markAsClaimed(foundId);
        return ResponseEntity.ok(item);
    }
}